package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.commons.utils.DateUtils;
import com.mail.dubbo.mapper.OmsOrderReturnApplyMapper;
import com.mail.model.db.OmsOrder;
import com.mail.model.db.OmsOrderReturnApply;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@DubboService
public class OmsOrderReturnApplyApiImpl implements OmsOrderReturnApplyApi {

    @Autowired
    private OmsOrderReturnApplyMapper returnApplyMapper;

    //页查询退货申请
    @Override
    public Page pageOrderReturn(Integer pageNum, Integer pageSize, OmsOrderReturnApply apply) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<OmsOrderReturnApply> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(apply.getHandleMan()), "handle_man", apply.getHandleMan())
                .eq(apply.getId() != null, "id", apply.getId())
                .eq(apply.getStatus() != null, "status", apply.getStatus())
                .orderByDesc("create_time");
        if (apply.getCreateTime() != null) {
//            String startCreate = DateUtils.getStartOfDay(Date.from(apply.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
            String endCreate = DateUtils.getEndOfDay(Date.from(apply.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
            qw.between("create_time", apply.getCreateTime(), endCreate);
        }
        if (apply.getHandleTime() != null) {
            String startHandle = DateUtils.getStartOfDay(Date.from(apply.getHandleTime().atZone(ZoneId.systemDefault()).toInstant()));
            String endHandle = DateUtils.getEndOfDay(Date.from(apply.getHandleTime().atZone(ZoneId.systemDefault()).toInstant()));
            qw.between("handle_time", startHandle, endHandle);
        }
//        if (StringUtils.isNotEmpty(apply.getReturnName())) {
//            qw.like("return_name", apply.getReturnName());//退货申请人
//        }
//        if (StringUtils.isNotEmpty(apply.getReturnPhone())) {
//            qw.eq("return_phone", apply.getReturnPhone());//退货申请人手机号码
//        }
        return returnApplyMapper.selectPage(page, qw);
    }

    //根据退货申请id，查询退货申请详情
    @Override
    public OmsOrderReturnApply findById(Long id) {
        return returnApplyMapper.selectById(id);
    }

    //根据退货申请id，修改退货申请状态
    @Override
    public Integer update(OmsOrderReturnApply orderReturnApply) {
        return returnApplyMapper.updateById(orderReturnApply);
    }

    //批量删除退货申请
    @Override
    public Integer delete(List<Long> ids) {
        return returnApplyMapper.deleteBatchIds(ids);
    }
}
